using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class FloatingPointProductionSpecification {
        [Test]
        public void ShouldParseFloatingPointNumber () {
            AssertFloatingPointNumberParsed (543.785, "543.785", 7);
            AssertFloatingPointNumberParsed (543.785, "543.785a", 7);
        }

        [Test]
        public void ShouldNotParse () {
            AssertFloatingPointNumberNotParsed (".785");
            AssertFloatingPointNumberNotParsed ("543.");
            AssertFloatingPointNumberNotParsed ("543");
            AssertFloatingPointNumberNotParsed ("abc");
            AssertFloatingPointNumberNotParsed ("234.bac");
        }

        private static void AssertFloatingPointNumberParsed (double expectedValue, string source, int length) {
            var floating = FloatingPointProduction.CreateTerminal ();

            var result = floating.Parse (source, 0, new ParseContext ());

            Assert.IsNotNull (result);
            Assert.AreEqual (length, result.Index);
            var term = result.Term as FloatingPointTerm;
            Assert.IsNotNull (term);
            Assert.AreEqual (expectedValue, term.Value);
            Assert.AreEqual (new SourceInformation (null, 0, length), term.SourceInformation);
        }

        private static void AssertFloatingPointNumberNotParsed (string source) {
            var floating = FloatingPointProduction.CreateTerminal ();

            var result = floating.Parse (source, 0, new ParseContext ());

            Assert.IsNull (result);
        }

        [Test]
        public void NameShouldBeReal () {
            IProduction p = FloatingPointProduction.CreateTerminal ();
            Assert.That (p.Name, Is.EqualTo ("real"));
        }
    }
}